package com.brillilab.service.core.lab.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.brillilab.common.entity.PageVo;
import com.brillilab.common.utils.MyBeanUtils;
import com.brillilab.dao.mapper.lab.LabApplyMapper;
import com.brillilab.dao.mapper.user.UserInviteAuditMapper;
import com.brillilab.domain.enums.apply.LabApplyEnum;
import com.brillilab.domain.po.lab.LabApply;
import com.brillilab.domain.vo.lab.LabApplyInforVo;
import com.brillilab.service.core.lab.ILabApplyService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 实验室申请表 服务实现类
 * </p>
 *
 * @author zlb
 * @since 2019-06-25
 */
@Service
@Transactional
public class LabApplyServiceImpl implements ILabApplyService {

    @Resource
    private LabApplyMapper labApplyMapper;

    @Resource
    private UserInviteAuditMapper userInviteAuditMapper;


    @Override
    public LabApply insert(LabApply labApply) {
        Integer b = labApplyMapper.insert(labApply);
        return b == 1 ? labApply : null;
    }

    @Override
    public boolean updateById(LabApply labApply) {
        Integer b = labApplyMapper.updateById(labApply);
        return b == 1 ? true : false;
    }

    @Override
    public LabApply selectById(Long id) {
        return labApplyMapper.selectById(id);
    }

    @Override
    public List<LabApply> selectListByState(Integer state) {
        LambdaQueryWrapper<LabApply> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(LabApply::getState, state);
        return labApplyMapper.selectList(wrapper);
    }

    @Override
    public LabApply selectByMobile(String mobile) {
        Assert.isTrue(StringUtils.isEmpty(mobile) == false, "参数缺失！");
        return labApplyMapper.selectOne(
                new LambdaQueryWrapper<LabApply>()
                        .eq(LabApply::getMobile, mobile));
    }

    @Override
    public PageVo<LabApply> selectPageVo(Page<LabApply> page, Wrapper<LabApply> wrapper) {
        IPage<LabApply> iPage = labApplyMapper.selectPage(page, wrapper);
        return new PageVo<LabApply>(iPage);
    }


    @Override
    public LabApply selectUser(String mobile, Integer state) {
        LambdaQueryWrapper<LabApply> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(LabApply::getMobile, mobile);
        if (state != null) {
            wrapper.eq(LabApply::getState, state);
        }
        return labApplyMapper.selectOne(wrapper);
    }

    @Override
    public LabApply selectUserByPIMobile(String piMobile, Integer state) {
        LambdaQueryWrapper<LabApply> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(LabApply::getPiMobile, piMobile);
        if (state != null) {
            wrapper.eq(LabApply::getState, state);
        }
        List<LabApply> labApplyList = labApplyMapper.selectList(wrapper);
        return CollectionUtils.isEmpty(labApplyList)==false ? labApplyList.get(0) : null;
    }

    @Override
    public PageVo<LabApply> selectByState(Integer applyState, String mobile, int pageNum, int pageSize) {
        IPage<LabApply> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<LabApply> queryWrapper = new LambdaQueryWrapper<>();
        if (applyState == null && StringUtils.isEmpty(mobile)) {
            IPage<LabApply> iPage = labApplyMapper.selectPage(page, queryWrapper);
            return new PageVo<LabApply>(iPage);
        }
        if (applyState != null) {
            queryWrapper.eq(LabApply::getState, applyState);
        }
        if (!StringUtils.isEmpty(mobile)) {
            queryWrapper.eq(LabApply::getMobile, mobile);
        }
        IPage<LabApply> iPage = labApplyMapper.selectPage(page, queryWrapper);
        return new PageVo<LabApply>(iPage);
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<LabApply> selectListByMobile(String mobile,boolean isAsc) {
        return labApplyMapper.selectList(
                new LambdaQueryWrapper<LabApply>()
                        .eq(LabApply::getMobile, mobile.trim())
                        .orderByAsc(isAsc,LabApply::getUpdateTime));
    }


    @Override
    public LabApplyInforVo selectByApplyId(Integer applyId) {
        LabApply labApply = labApplyMapper.selectOne(
                new LambdaQueryWrapper<LabApply>()
                        .eq(LabApply::getId, applyId));
        return MyBeanUtils.copyBean(labApply, LabApplyInforVo.class);
    }

    @Override
    public LabApply selectAuditPassOne(String mobile) {
        return labApplyMapper.selectOne(
                new LambdaQueryWrapper<LabApply>()
                        .eq(LabApply::getMobile, mobile)
                        .eq(LabApply::getState, LabApplyEnum.State.AUDIT_PASS.getValue()));
    }
}
